﻿using BulkUploadGovernanceFiles.Models;
using BulkUploadGovernanceFiles.MongoSpecification;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Bson.Serialization.Conventions;

namespace BulkUploadGovernanceFiles
{
    public class CorporateDataRepository
    {
        private readonly MongoCollection<CorporateData> _corporateDataCollection;

        public CorporateDataRepository()
        {
            var corporateDataConnectionString = ConfigManager.ClientDw.ConnectionString;

            var mongoClient = new MongoClient(corporateDataConnectionString);
            var server = mongoClient.GetServer();
            var database = server.GetDatabase(ConfigManager.ClientDw.DataBaseName);

            _corporateDataCollection = database.GetCollection<CorporateData>("CorporateData");
        }
        
        public CorporateData GetByCompanyId(ISpecification<CorporateData> specification)
        {
            
            //    .Select(x => new
            //{
            //    companyId = x.Id
            //    CompanyName = x.CompanyName
            //}).Where(x=>x.Id == );
            return _corporateDataCollection.AsQueryable<CorporateData>().FirstOrDefault(specification.SpecExpression);
        }

        public CorporateDataDto GetByCompanyId(int capitalIqId)
        {
            return _corporateDataCollection.AsQueryable()
                .Where(x => x.CapitalIqId == capitalIqId)
                .Select(x => new CorporateDataDto
                {
                    CompanyName = x.CompanyName,
                    Id = x.Id,
                    
                }).FirstOrDefault();
            //return new CorporateData();
        }

        //Select(x=>new
        //    {
        //       CompanyName = x.CompanyName
        //    }).
    }

    public class CorporateDataDto
    {
        public string CompanyName { get; set; }
        public Guid Id { get; set; }
    }
}
